package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.InfoSubscribeDTO;
import com.financing.platform.service.IInfoSubscribeService;
import com.financing.platform.util.StringUtil;
import com.financing.platform.vo.InfoSubscribeVO;

/**
 * 
 * @ClassName: InfoSubscribeAction
 * @Description: 信息订阅
 * @author lisu
 *
 */
@Controller("infoSubscribeActionAPI")
@RequestMapping("/api/infoSubscribe")
@Scope("prototype")
public class InfoSubscribeAction extends BaseAction {

	@Autowired
	private IInfoSubscribeService infoSubscribeServiceImpl;

	@SuppressWarnings("unchecked")
	@RequestMapping("/listInfoSubscribe.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request,
			HttpServletResponse response, InfoSubscribeDTO infoSubscribeDTO,Pager pager) {
		try {
			log.info("In InfoSubscribeAction list begin!");
			
			// 添加参数验证  
			if (StringUtil.isNullOrEmptyStr(infoSubscribeDTO.getSubType())) {
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}

			infoSubscribeDTO.setBegin(pager.getStartIndex());
			infoSubscribeDTO.setPageNo(pager.getPageNo());
			infoSubscribeDTO.setPageSize(pager.getPageSize());
			
			int count = infoSubscribeServiceImpl.queryInfoSubscribedListCount(infoSubscribeDTO);
			pager.setTotalCount(count);
			
			List<InfoSubscribeVO> list;
			
			if(count>0){
				list = infoSubscribeServiceImpl.queryInfoSubscribeList(infoSubscribeDTO);
			}else{
				list = Collections.EMPTY_LIST;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list", list);
			resultMap.setAttribute("pager", pager);
		} catch (Exception e) {
			log.error("ProjectCompanyAction list error !", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		log.info("In InfoSubscribeAction list end!");
		return resultMap;
	}
}
